﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using System.IO;
using System.Reflection;
using MSH.Common.Helpers;
using System.Threading.Tasks;

namespace MSH.Server.Classes
{
    public class FacebookSocialNetworkProvider : ISocialNetworkProvider
    {
        private static String authenticationFinishedURL = "http://www.facebook.com/connect/login_success.html";

        private String applicationID = null;
        private String secretKey = null;
        private byte[] logo = null;
        private String logoChecksum = null;
        private FacebookOAuthClient oAuthClient;

        /// <summary>
        /// De provider is verantwoordelijk voor het authenticeren met facebook, waardoor MSH gebruik kan maken van het facebook account.
        /// </summary>
        public FacebookSocialNetworkProvider()
        {
            applicationID = System.Configuration.ConfigurationManager.AppSettings["facebookApplicationID"].ToString();
            secretKey = System.Configuration.ConfigurationManager.AppSettings["facebookSecretKey"].ToString();

            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MSH.Server.Resources.Facebook logo.png");
            logo = new byte[stream.Length];
            stream.Read(logo, 0, (int)stream.Length);
            stream.Position = 0;
            logoChecksum = CommonHelper.GetMD5HashFromStream(stream);

            oAuthClient = new FacebookOAuthClient();
            oAuthClient.AppId = applicationID;
            oAuthClient.AppSecret = secretKey;

            SocialNetworkManager.RegisterProvider(this);
        }

        public bool ParseAuthenticationURL(String url, out String authToken)
        {
            authToken = null;
            FacebookOAuthResult oAuthResult;
            if (FacebookOAuthResult.TryParse(url, out oAuthResult))
            {             
                dynamic result = oAuthClient.ExchangeCodeForAccessToken(oAuthResult.Code);

                authToken = result.access_token;
           
                return !String.IsNullOrEmpty(authToken);
            }

            return false;
        }

        public string GenerateAuthenticationURL()
        {
            var loginParameters = new Dictionary<string, object>
                                      {
                                          { "response_type", "code" },
                                          { "display", "touch" }
                                      };
            Uri u = FacebookOAuthClient.GetLoginUrl(applicationID, null, new String[] 
                        { "offline_access", "user_about_me", "user_birthday", "user_education_history", "email", "user_location", 
                            "user_relationship_details", "user_religion_politics", "user_relationships", "user_website", "user_work_history"}
                        , loginParameters);  
            return u.ToString();
        }

        public string GetName()
        {
            return "Facebook";
        }

        public byte[] GetLogo()
        {
            return logo;
        }

        public string GetLogoChecksum()
        {
            return logoChecksum;
        }

        public string GetAuthenticationFinishedUrl()
        {
            return authenticationFinishedURL;
        }
    }
}
